package com.diligreat.action; 

import java.io.IOException;
import java.util.Map;

import org.springframework.util.StringUtils;

import com.diligreat.action.base.BaseAction;
import com.diligreat.domain.Admin;
import com.diligreat.util.ConfigFileUtil;
import com.diligreat.util.Constants;
import com.diligreat.util.EncryptUtil;
import com.opensymphony.xwork2.ModelDriven;

/**  
 * @Author Bruce 
 * @Date 2012-10-13
 * @Comments
 */
public class AdminAction extends BaseAction implements ModelDriven<Admin>{

	private static final long serialVersionUID = 1L;
	private Admin admin = new Admin();
	private String newPwd;
	private String newPwdConf;
	
	public String login(){
		
		return SUCCESS;
	}
	
	public String login_submit() throws Exception{
		getSession().put(Constants.SESSION_KEY_OP, admin);
		return SUCCESS;
	}
	public String logout(){
		Map<String, Object> session =  getSession();
		Admin admin = (Admin) session.get(Constants.SESSION_KEY_OP);
		if(admin != null){
			session.remove(Constants.SESSION_KEY_OP);
		}
		return SUCCESS;
	}
	
	public String modifyPwd(){
		return SUCCESS;
	}
	public String modifyPwdSubmit() throws IOException, Exception{
		Admin currAdmin = (Admin) getSession().get(Constants.SESSION_KEY_OP);
		ConfigFileUtil.setValue(currAdmin.getOpName(), EncryptUtil.encode(newPwd));
		
		return SUCCESS;
	}
	public String main(){
		
		return SUCCESS;
	}

	@Override
	public Admin getModel() {
		
		return admin;
	}
	//============================ 校验方法 ==================================
	@Override
	public void validate() {
		//System.out.println("validate()");
		super.validate();
	}
	public void validateLogin_submit() throws Exception{
		if(!StringUtils.hasText(admin.getOpName())){
			addActionError("用户名不能为空！");
		}else if(!StringUtils.hasText(admin.getPassword())){
			addActionError("密码不能为空！");
		}else{
			String pwdEncodeStr = ConfigFileUtil.getValue(admin.getOpName());
			if(pwdEncodeStr == null){
				addActionError("用户名/密码不匹配！");
				//System.out.println("用户名不存在");
			}else if(!EncryptUtil.encode(admin.getPassword()).equals(pwdEncodeStr)){
				addActionError("用户名/密码不匹配！");
				//System.out.println("密码不正确");
			}
		}
	}
	
	public void validateModifyPwdSubmit() throws Exception{
		Admin currAdmin = (Admin) getSession().get(Constants.SESSION_KEY_OP);
		if(currAdmin == null || !StringUtils.hasText(currAdmin.getOpName())){
			addActionError("登录超时，请重新登录！");
		}else{
			if(!StringUtils.hasText(admin.getPassword())){
				addActionError("原密码不能为空！");
			}else if(!StringUtils.hasText(newPwd)){
				addActionError("新密码不能为空！");
			}else{
				String pwdEncodeStr = ConfigFileUtil.getValue(currAdmin.getOpName());
				if(!EncryptUtil.encode(admin.getPassword()).equals(pwdEncodeStr)){
					addActionError("原密码不正确！");
				}else if(!StringUtils.hasText(newPwdConf)){
					addActionError("确认密码不能为空！");
				}else if(!newPwd.equals(newPwdConf)){
					addActionError("新密码与确认密码不一致！");
				}
			}
		}
	}

	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}

	public void setNewPwdConf(String newPwdConf) {
		this.newPwdConf = newPwdConf;
	}

	
}
 